package com.myapp.ruyixing.ui;

import java.util.List;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

import com.myapp.ruyixing.R;
import com.myapp.ruyixing.http.RYHttpClient;
import com.myapp.ruyixing.http.RYHttpResponseHandler;
import com.myapp.ruyixing.model.Airline;
import com.myapp.ruyixing.model.Airline.AirlineSearchItem;
import com.myapp.ruyixing.ui.widget.TitleBar;

public class AirTicketSearchResultActivity extends BaseActivity implements
		OnClickListener, OnItemClickListener {

	ListView mListView;
	TextView mEmptyView;
	View mLoadingView;

	ToggleButton mFocusedTabView;
	RelativeLayout mBottomTabView;

	String mFromCity;
	String mToCity;
	String mFromCityName;
	String mToCityName;
	String mStartDate;
	int mCarbins;
	int mTimeSort;
	int mPriceSort;

	private RYHttpResponseHandler mHandler = new RYHttpResponseHandler() {

		@Override
		public void onSuccess(Object o) {
			Airline airlines = (Airline) o;
			if (airlines != null && airlines.list != null) {

				MyListAdapter adapter = new MyListAdapter(airlines.list);
				mListView.setAdapter(adapter);
				mListView.setEmptyView(mEmptyView);

			}
			
			hideLoading();
		}

		@Override
		public void onFailure(Throwable throwable) {
			if (!isFinishing()) {
				Toast.makeText(getApplicationContext(), "加载失败",
						Toast.LENGTH_SHORT).show();
			}

			hideLoading();
		}
	};

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);

		setContentView(R.layout.activity_air_ticket_search_result);

		initView();

		mFromCity = getIntent().getStringExtra("from");
		mToCity = getIntent().getStringExtra("to");
		mStartDate = getIntent().getStringExtra("date");
		mCarbins = getIntent().getIntExtra("carbins", 0);
		mFromCityName = getIntent().getStringExtra("fromCityName");
		mToCityName = getIntent().getStringExtra("toCityName");

		((TitleBar) findViewById(R.id.title_bar)).setTitleText(mFromCityName + "-"
				+ mToCityName);

		reqeust();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.cost_price:
			mPriceSort = mPriceSort == 0 ? 1 : mPriceSort == 1 ? 2 : 1;
			mTimeSort = 0;
			changeFocusedTab(v, mPriceSort == 2);

			reqeust();

			break;
		case R.id.cost_time:
			mTimeSort = mTimeSort == 0 ? 1 : mTimeSort == 1 ? 2 : 1;
			mPriceSort = 0;
			changeFocusedTab(v, mTimeSort == 2);

			reqeust();

			break;
		case R.id.tab_today:
		case R.id.tab_tomorrow:
		case R.id.tab_after_tomorrow:
		case R.id.tab_next_day:
			changeBottomTabs(v);

			break;

		default:
			break;
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		if (position >= 0 && position < mListView.getAdapter().getCount()) {
			AirlineSearchItem item = (AirlineSearchItem) mListView.getAdapter()
					.getItem(position);
			item.date = mStartDate;
			item.fromName = mFromCityName;
			item.toName = mToCityName;
			item.fromCity = mFromCity;
			item.toCity = mToCity;
			startActivityForResult(new Intent(this,
					AirTicketSubmitActivity.class).putExtra("data", item), 0);
		}
	}

	@Override
	protected void onActivityResult(int arg0, int arg1, Intent arg2) {
		super.onActivityResult(arg0, arg1, arg2);

		if (arg1 == RESULT_OK) {
			setResult(RESULT_OK);
			finish();
		}
	}

	private void showLoading() {
		mLoadingView.setVisibility(View.VISIBLE);
	}

	private void hideLoading() {
		mLoadingView.setVisibility(View.GONE);
	}
	
	private void reqeust() {
		showLoading();
		RYHttpClient.getAirlines(this, mHandler, mFromCity, mToCity,
				mStartDate, "1", mCarbins, 0, "0", mTimeSort, mPriceSort);
	}

	private void initView() {
		mListView = (ListView) findViewById(android.R.id.list);
		mEmptyView = (TextView) findViewById(android.R.id.empty);
		mLoadingView = findViewById(R.id.my_loading);

		mListView.setOnItemClickListener(this);

		findViewById(R.id.cost_sort).setOnClickListener(this);
		findViewById(R.id.cost_price).setOnClickListener(this);
		findViewById(R.id.cost_time).setOnClickListener(this);

		findViewById(R.id.tab_today).setOnClickListener(this);
		findViewById(R.id.tab_tomorrow).setOnClickListener(this);
		findViewById(R.id.tab_after_tomorrow).setOnClickListener(this);
		findViewById(R.id.tab_next_day).setOnClickListener(this);

		mFocusedTabView = (ToggleButton) findViewById(R.id.cost_sort);
		mBottomTabView = (RelativeLayout) findViewById(R.id.tab_today);

		((TitleBar) findViewById(R.id.title_bar))
				.setOnIconClickedListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						onBackPressed();
					}
				});
	}

	private void changeFocusedTab(View v, boolean selected) {
		if (mFocusedTabView != v) {
			if (mFocusedTabView != null) {
				mFocusedTabView.setChecked(false);
				mFocusedTabView.setSelected(false);
				setAsc();
			}
		}

		mFocusedTabView = (ToggleButton) v;
		mFocusedTabView.setChecked(true);
		if (selected) {
			setDsc();
		} else {
			setAsc();
		}
	}

	private void setDsc() {
		Drawable suggetionDrawable = getResources().getDrawable(
				R.drawable.icon_air_ticket_08_down);
		mFocusedTabView.setCompoundDrawablesWithIntrinsicBounds(
				null, null, suggetionDrawable, null);
	}

	private void setAsc() {
		Drawable suggetionDrawable = getResources().getDrawable(
				R.drawable.icon_air_ticket_08);
		mFocusedTabView.setCompoundDrawablesWithIntrinsicBounds(
				null, null, suggetionDrawable, null);
	}

	private void changeBottomTabs(View v) {
		if (mBottomTabView.getId() == v.getId()) {
			return;
		}

		mBottomTabView.setBackgroundColor(getResources().getColor(
				R.color.bottom_nav_bg));
		for (int i = 0; i < mBottomTabView.getChildCount(); i++) {
			mBottomTabView.getChildAt(i).setEnabled(false);
		}
		mBottomTabView = (RelativeLayout) v;
		mBottomTabView.setBackgroundColor(getResources().getColor(
				R.color.text_normal_white));
		for (int i = 0; i < mBottomTabView.getChildCount(); i++) {
			mBottomTabView.getChildAt(i).setEnabled(true);
		}
	}

	private class ViewHolder {
		TextView mStartTimeView;
		TextView mArriveTimeView;
		TextView mOnTimeRateView;
		TextView mFromToView;
		TextView mAirplaneView;
		TextView mPriceView;
	}

	private class MyListAdapter extends ArrayAdapter<AirlineSearchItem> {

		public MyListAdapter(List<AirlineSearchItem> airlines) {
			super(AirTicketSearchResultActivity.this, -1, airlines);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = LayoutInflater.from(getContext()).inflate(
						R.layout.cell_air_ticket_search_result, parent, false);

				ViewHolder holder = new ViewHolder();
				holder.mStartTimeView = (TextView) convertView
						.findViewById(R.id.start_time);
				holder.mPriceView = (TextView) convertView
						.findViewById(R.id.price);
				holder.mArriveTimeView = (TextView) convertView
						.findViewById(R.id.arrive_time);
				holder.mOnTimeRateView = (TextView) convertView
						.findViewById(R.id.on_time_rate);
				holder.mFromToView = (TextView) convertView
						.findViewById(R.id.from_to);
				holder.mAirplaneView = (TextView) convertView
						.findViewById(R.id.airplane);

				convertView.setTag(holder);
			}

			ViewHolder mHolder = (ViewHolder) convertView.getTag();

			AirlineSearchItem item = getItem(position);

			mHolder.mStartTimeView.setText(item.getTimeStart() + "——");
			mHolder.mArriveTimeView.setText(item.getTimeArrive());
			mHolder.mPriceView.setText("¥" + item.price_go);
			// mHolder.mOnTimeRateView.setText("准点率" + item.onTimeRate + "%");
			mHolder.mFromToView.setText(item.airportName_go);
			mHolder.mAirplaneView.setText(item.planeinfo_go);

			return convertView;

		}
	}
}
